package net.danielpaz.pucc.poo.trabalho.model;
import java.sql.*;


public class AlbumDAO {
    public Album album;
    public BD bd;
    private PreparedStatement statement;
    private ResultSet resultSet;
    private String men, sql;
    public static final byte INCLUSAO = 1;
    public static final byte ALTERACAO = 2;
    public static final byte EXCLUSAO = 3;

    public AlbumDAO() {
        bd = new BD();
    //    album = new Album();
    }

    
    public boolean localizar(int operacao, Album album) {
        sql = "select * from album where id = ? ";
        try{
            statement = bd.connection.prepareStatement(sql);
            statement.setString(1, Integer.toString(album.getId()));
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
            	 album.setId(Integer.parseInt(resultSet.getString(1)));
                 album.setNome(resultSet.getString(2));
                 album.setIdCantor(Integer.parseInt(resultSet.getString(3)));
                 album.setIdMidia(Integer.parseInt(resultSet.getString(4)));
                 album.setPreco(Double.parseDouble(resultSet.getString(5)));
                 album.setQuantidade(Integer.parseInt(resultSet.getString(6)));
            }
            return true;
        }
        catch (SQLException erro) {
        	erro.printStackTrace();
            return false;
        }
    }

    
    public String atualizar(int operacao, Album album) {
        men = "Operacao realizada com sucesso!";
        try {
            if (operacao==INCLUSAO){
                sql = "insert into album values (?,?,?,?,?,?)";
                statement = bd.connection.prepareStatement(sql);
                statement.setString(1, Integer.toString(album.getId()));
                statement.setString(2, album.getNome());
                statement.setString(3, Integer.toString(album.getIdCantor()));
                statement.setString(4, Integer.toString(album.getIdMidia()));
                statement.setString(5, Double.toString(album.getPreco()));
                statement.setString(6, Integer.toString(album.getQuantidade()));
            }
            else if (operacao==ALTERACAO){
                sql = "update album set nome = ?, idcantor = ?, idmidia = ?, preco = ?, quantidade = ? where id = ?";
                statement = bd.connection.prepareStatement(sql);
                statement.setString(1, album.getNome());
                statement.setString(2, Integer.toString(album.getIdCantor()));
                statement.setString(3, Integer.toString(album.getIdMidia()));
                statement.setString(4, Double.toString(album.getPreco()));
                statement.setString(5, Integer.toString(album.getQuantidade()));
                statement.setString(6, Integer.toString(album.getId()));
            }
            else if (operacao==EXCLUSAO){
                sql = "delete from album where id = ?";
                statement = bd.connection.prepareStatement(sql);
                statement.setString(1, Integer.toString(album.getId()));
            }
            if (statement.executeUpdate() == 0)
                men = "Falha na operacao!";
        } catch (SQLException erro) {
            men = "Falha na operacao " + erro.toString();
        }
        return men;
    }
    
}
